package com.sjm.bookshop.controller.userctroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjm.bookshop.entity.Admin;
import com.sjm.bookshop.service.userservice.AdminService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //    查询用户列表
    @RequestMapping("/selectAllAdmin")
    public Result getUser(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Admin> AdminList = adminService.AdminList();
        PageInfo<Admin> pageInfo = new PageInfo<>(AdminList);
        return Result.success(200, pageInfo, "success");
    }

    //        模糊查询
    @RequestMapping("/selectByKeyWord")
    public Result getUser(String keyWord, int page, int limit) {
        if (keyWord.equals(null) || keyWord != null) {
            PageHelper.startPage(page, limit);
            List<Admin> AdminList = adminService.selectSomeOne(keyWord);
            PageInfo<Admin> pageInfo = new PageInfo<>(AdminList);
            System.out.println(keyWord);
            return Result.success(200, pageInfo.getList(), "success");
        } else {
            return Result.fail(201, "关键字不能为空");
        }
    }

    //    删除用户
    @RequestMapping("/deleteAdmin/{adminId}/{jurisdiction}")
    public Result deleteAdmin(@PathVariable("adminId") int adminId, @PathVariable("jurisdiction") int jurisdiction) {
        if (jurisdiction == 1 || jurisdiction == 2) {
            int res = adminService.deleteOne(adminId);
            if (res > 0) {
                return Result.success(200, "成功删除" + res + "条记录");
            } else {
                return Result.fail(201, "删除失败");
            }
        } else {
            return Result.fail(201, "当前用户无法进行此操作");
        }

    }


    //    修改状态
    @RequestMapping("/change")
    public Result changeStatus(int adminId, int adminstatus) {
        if (adminstatus == 0 || adminstatus == 1) {
            int res = adminService.changeAdminStatus(adminId, adminstatus);
            if (res > 0) {
                return Result.success(200, "修改成功");
            } else {
                return Result.fail(201, "修改失败");
            }
        }
        return Result.fail(201, "修改失败");
    }

    //    修改用户
    @RequestMapping("/Update")
    public Result changeAdminInfo(@RequestBody Admin admin) {
        if (admin != null) {
            if (adminService.UpdateAdminInfo(admin) > 0) {
                return Result.success(200, "成功修改了一条记录");
            } else {
                return Result.fail(201, "修改失败");
            }
        } else {
            return Result.fail(201, "fail");
        }
    }




}